"""
Benchmarking heapsort

shell> echo $PYTHONPATH
/home/disk04/jyrki/CPHSTL/Tool/Benchmark/
"""

__author__ = "Jyrki Katajainen"
__email__ = "jyrki@diku.dk"
__version__ = "$Revision: 1.1 $"[11:-2]
__date__ = "$Date: 2005/01/26 11:55:51 $"[7:-2]

import benchmark
import os

class sort_case(benchmark.case):
  def __init__(self, n, element, function, directory):
    benchmark.case.__init__(self)
    self.n = n
    self.computer = 'pc-014.diku.dk'
    self.compiler = '/scratch/g++3.0.4/bin/g++' 
#    self.compiler = '/usr/bin/g++-3.2' 
    self.compiler_options.extend(['-O3'])
    home = os.environ['HOME']
    self.include_paths.extend([
      home + '/CPHSTL/Report/Local-heaps/Programs/Common/',
      home + '/CPHSTL/Report/Local-heaps/Program/' + directory + '/' 
      ])
    self.include_files.extend(['<vector>', '<functional>', function + '.c++'])
    self.dual_exists = 1
    self.constructor_call = \
      'experiment<' + \
         element + ',' + \
         'std::less<' + element + '>,' +\
        'std::vector<' + element + '>,' +\
        'std::vector<' + element + '>::iterator' +\
      '>(' + str(n) + ')'
    self.time_unit = 'ns'
    self.driver_file = self.generate_cpu_time_driver()

  def output(self):
    return (self.n, float(self.driver_output) / self.n)

#  def tidy_up(self): 
#    "Used for debugging to see the files generated"
#    pass 
  
class sort_curve(benchmark.curve_suite):
  def __init__(self, element, function, directory, log_i, log_k):
    benchmark.curve_suite.__init__(self)
    self.title = directory + ' ' + function
    for j in range(log_i, log_k + 1):
      for k in range(0, 100, 100):
        self.add(sort_case(k + (1 << j), element, function, directory))

class sort_plot(benchmark.plot_suite):
  def __init__(self, element, log_i, log_k):
    benchmark.plot_suite.__init__(self)
    self.title = 'Efficiency of various sorting functions for random integers' 
    self.xlabel = 'n'
    self.ylabel = 'Execution time per element [in nanoseconds]'
    self.add(sort_curve(element, 'heapsort', '3-ary', log_i, log_k))
    self.add(sort_curve(element, 'sort', 'SGI', log_i, log_k))
    self.add(sort_curve(element, 'partial_sort', 'SGI', log_i, log_k))
    self.gnuplot_commands += """
set title '%(title)s'
set xlabel '%(xlabel)s'
set ylabel '%(ylabel)s'
set logscale x
""" % self.__dict__

if __name__ == '__main__':
  benchmark.main(
    task = sort_plot('int', 10, 11), #23
    runner = benchmark.gnuplot_runner
  )
